﻿
CREATE PROCEDURE [dbo].[resuma_ImportPersSal]
	@CodUnitate smallint
AS
BEGIN
	DECLARE @Cod_Unit decimal(18,0)
	SET @Cod_Unit = 281479271679015 
	
	SET NOCOUNT ON;
	DELETE FROM Arhive WHERE CodUnitate=@CodUnitate
	INSERT INTO [Arhive]
           ([Denumire]
           ,[CodUnitate]
           ,[DataInceput]
           ,[DataSfarsit]
           ,[Anul]
           ,[BalCon]
           ,[Fares]
           ,[Luna]
           ,[ResUma])
     SELECT 
           CAST(Anul AS Varchar(4)) + ',' +  CAst(Luna as varchar(2))
           ,@CodUnitate
           ,CAST(CAST(Anul AS Varchar(4))  + '-' +  CAst(Luna as varchar(2))  + '-01' As Datetime)
           ,DATEADD(day,-1,DATEADD(month,1,CAST(CAST( Anul AS Varchar(4))  + '-' +  CAst(Luna as varchar(2))  + '-01' As Datetime)))
           ,Anul
           ,0
           ,0
           ,Luna
           ,1 
           FROM PersSal.dbo.pArhCfgLuna  pArh WHERE
           Cod_Unit = @Cod_Unit




	/*DELETE FROM [Import_Persoane]
    INSERT INTO [Import_Persoane]
           ([NumePrenume]
           ,[Nume_Anterior]
           ,[Cnp]
           ,[Sex]
           ,[DataNasterii]
           ,[IDLocNastere]
           ,[TipAI]
           ,[SeriaAI]
           ,[NumarAI]
           ,[DataElibAI]
           ,[EmitentAI]
           ,[NumeTata]
           ,[NumeMama]
           ,[StradaAdr]
           ,[IDLocalitateAdr]
           ,[NrAdr]
           ,[BlocAdr]
           ,[EtajAdr]
           ,[ScaraAdr]
           ,[ApartamentAdr]
           ,[GradInvaliditate]
           ,[TelefonFix]
           ,[TelefonMobil]
           ,[Observatii]
           ,[NumarCM]
           ,[SerieCM]
           ,[CodUnitate]
           ,[NrFileCM]
           ,[NrCutieCM]
           ,[Marca]
           ,[TelefonInterior]
           ,[IdCasaAsigSanatate]
           ,[IdTipStudii]
           ,[DataVechimeMunca]
           ,[PozCutieCM]
           ,[PlatesteCAS]
           ,[PlatesteCASS]
           ,[PlatesteSomaj]
           ,[PlatesteImpozit]
           ,[AreDeducere]
           ,[DataPlecarii]
           ,[MotivIncetareContract]
           ,[Operatie]
           ,[Selectat])
     SELECT  LTRIM(RTRIM([Nume])) + ' ' + LTRIM(RTRIM([Prenume]))  as Nume_Prenume,
     LTRIM(RTRIM(Anterior)),pp.[Cnp],pp.Sex,[DataNast],
     COALESCE(
     (SELECT TOP 1 IDLocalitate FROM Localitati WHERE (Nume like '%' + LN_Loc + '%') OR (Nume like '%' + LN_Sat + '%')),0) IDLocalitate,
     AI_Tip, AI_Ser, AI_Nr,COALESCE(AI_DatEl,GETDATE()) [DataElibAI] ,AI_Elib,
     '' AS NumeMama, '' AS NumeTata, Adr_Str,
     COALESCE((SELECT TOP 1 IDLocalitate FROM Localitati WHERE (Nume like '%' + Adr_Loc + '%') OR (Nume like '%' + Adr_Sat + '%')),0) IDLocalitateAdr,
     Adr_Nr,Adr_Bloc,Adr_Et, Adr_Sc, Adr_Ap,
     Handic as GradInvaliditate, Adr_Tel,Adr_Tel, Mentiuni AS Observatii,
     CMNr,CMSer,@CodUnitate,0 AS NrFile, 0 AS NrCutie,pp.Marca,  '' as Telefoninterior, CodCasaSan,
     CodStudii,COALESCE(COALESCE(DataPrAnga,DataUnit),GETDATE()) as [DataVechimeMunca], 0 AS PozCutieCM,  
     1 AS PlatesteCAS,ScutAsSA=1, ScutSomA,ScutImpA,0 as arededucere,DataPlec,
     artPlec + '-' + litPlec as MotivIncetare,0,1 AS Selectat  
     FROM PersSal.dbo.pPerson pp LEFT OUTER JOIN [Persoane] pr ON pp.CNP = pr.Cnp AND pr.CodUnitate = @CodUnitate
     JOIN PersSal.dbo.pPersSal pe ON pp.Marca=pe.Marca
     WHERE  pr.IdPersoana is null*/
	
END